#include<iostream>
using namespace std;

const int N=5001;
int arr[N][N], sign[N][N];

int main()
{
	int n, m; cin>>n>>m;
	while(n--)
	{
		int x, y, v;
		cin>>x>>y>>v;
		x++; y++;
		if(x<=N && y<=N)arr[x][y]+=v;
	}
	for(int i=1; i<=N; i++)
	{
		for(int j=1; j<=N; j++)
		{
			sign[i][j]=arr[i][j]+sign[i-1][j]+sign[i][j-1]-sign[i-1][j-1];
		}
	}
	int ret=0;
	m=min(m, N);
	for(int i=m; i<=N; i++)
	{
		for(int j=m; j<=N; j++)
		{
			int x2=i, y2=j, x1=i-m+1, y1=j-m+1;
			ret=max(ret, sign[x2][y2]-sign[x2][y1-1]-sign[x1-1][y2]+sign[x1-1][y1-1]);
		}
	}
	cout<<ret;
	return 0;
}
